# Copyright 2018 Hippo Seven
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.4.1)
project(image-png C)

set(LIBPNG_SOURCES
    image_png.c
    libpng/png.c
    libpng/pngerror.c
    libpng/pngget.c
    libpng/pngmem.c
    libpng/pngpread.c
    libpng/pngread.c
    libpng/pngrio.c
    libpng/pngrtran.c
    libpng/pngrutil.c
    libpng/pngset.c
    libpng/pngtrans.c
)
set(LIBPNG_INCLUDES
    ${IMAGE_EXPORT_INCLUDES}
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/libpng
)
set(LIBPNG_DEFINITIONS
)
set(LIBPNG_LIBRARIES
    z
)

if (${ANDROID_ABI} STREQUAL "armeabi-v7a")

  enable_language(ASM)

  set(LIBPNG_SOURCES
      ${LIBPNG_SOURCES}
      libpng/arm/arm_init.c
      libpng/arm/filter_neon.S
  )
  set(LIBPNG_INCLUDES
      ${LIBPNG_INCLUDES}
      ${ANDROID_NDK}/sources/android/cpufeatures
  )
  set(LIBPNG_DEFINITIONS
      ${LIBPNG_DEFINITIONS}
      -DPNG_ARM_NEON_API_SUPPORTED
      -DPNG_ARM_NEON_CHECK_SUPPORTED
      -DPNG_ARM_NEON_OPT=1
      -DPNG_ARM_NEON_IMPLEMENTATION=2
      -DPNG_ARM_NEON_FILE=\"contrib/arm-neon/android-ndk.c\"
  )
  set(LIBPNG_LIBRARIES
      ${LIBPNG_LIBRARIES}
      cpufeatures
  )

elseif (${ANDROID_ABI} STREQUAL "arm64-v8a")

  enable_language(ASM)

  set(LIBPNG_SOURCES
      ${LIBPNG_SOURCES}
      libpng/arm/arm_init.c
      libpng/arm/filter_neon_intrinsics.c
      libpng/arm/palette_neon_intrinsics.c
  )
  set(LIBPNG_INCLUDES
      ${LIBPNG_INCLUDES}
      ${ANDROID_NDK}/sources/android/cpufeatures
  )
  set(LIBPNG_DEFINITIONS
      ${LIBPNG_DEFINITIONS}
      -DPNG_ARM_NEON_API_SUPPORTED
      -DPNG_ARM_NEON_CHECK_SUPPORTED
      -DPNG_ARM_NEON_OPT=1
      -DPNG_ARM_NEON_IMPLEMENTATION=1
      -DPNG_ARM_NEON_FILE=\"contrib/arm-neon/android-ndk.c\"
  )
  set(LIBPNG_LIBRARIES
      ${LIBPNG_LIBRARIES}
      cpufeatures
  )

endif ()

if(IMAGE_SINGLE_SHARED_LIB)
    # Compile a static library for image-singlelib
    set(LIBPNG_LIBRARY_NAME image-png-static)
    set(LIBPNG_LIBRARY_TYPE STATIC)
    set(LIBPNG_INCLUDES_VISIBILITY PUBLIC) # Share include dir with image project
else()
    # Compile a shared library to be imported by image-core
    set(LIBPNG_LIBRARY_NAME image-png)
    set(LIBPNG_LIBRARY_TYPE SHARED)
    set(LIBPNG_INCLUDES_VISIBILITY PRIVATE)
    
    # Link against image shared lib to get helper methods
    set(LIBPNG_LIBRARIES
        ${LIBPNG_LIBRARIES}
        image
        log
    )
endif()

add_library(${LIBPNG_LIBRARY_NAME} ${LIBPNG_LIBRARY_TYPE} ${LIBPNG_SOURCES})
target_include_directories(${LIBPNG_LIBRARY_NAME} ${LIBPNG_INCLUDES_VISIBILITY} ${LIBPNG_INCLUDES})
target_compile_definitions(${LIBPNG_LIBRARY_NAME} PRIVATE ${LIBPNG_DEFINITIONS})
target_link_libraries(${LIBPNG_LIBRARY_NAME} PRIVATE ${LIBPNG_LIBRARIES})